package com.passer.uam;

import com.passer.toolbox.common.core.util.UUIDUtils;
import com.passer.uam.config.UamConfig;
import com.passer.uam.entity.Account;
import com.passer.uam.manager.account.AccountManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Scanner;

public class UamLauncher {
    private static Scanner scanner;
    private static AccountManager accountManager;

    public static void main(String[] args) {
        // System.out.println("软件初始化...");
        init();
        // System.out.println("1.搜索账号");
        // System.out.println("2.保存账号");
        // System.out.println("3.删除账号");

        // for (int i = 31; i < 37; i++) {
        //     System.out.print("\r\33[" + i + ";1m我是博主\33[0m\t" + i);
        //     // System.out.print("\r还是大部分时间开发");
        //     try {
        //         Thread.sleep(1000);
        //     } catch (InterruptedException e) {
        //         throw new RuntimeException(e);
        //     }
        // }

        showMenu();

        String type = args[0];

        switch (type) {
            case "list":
                break;
            case "search":
                searchAccount();
                break;
            case "save":
                saveAccount();
                break;
            case "delete":
                break;
            default:
                break;
        }
        scanner.close();
    }

    private static void init() {
        UamConfig.init();
        scanner = new Scanner(System.in);
        accountManager = new AccountManager();
    }

    private static String showMenu() {
        boolean finished = false;
        String menu = "\r\33[34;1m搜索账号\33[0m\t保存账号\t删除账号";
        String input;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("欢迎使用个人账号管理软件！");
        while (!finished) {
            System.out.print(menu);

            try {

                int read = reader.read();
                System.out.println((char) read);

            } catch (IOException e) {


                e.printStackTrace();


            }
        }
        return null;
    }

    private static void saveAccount() {
        Account account = new Account();
        System.out.println("请输入账户信息");
        System.out.print("账号来源：");
        String source = scanner.next();
        scanner.nextLine();
        System.out.print("网址：");
        String url = scanner.nextLine();
        System.out.print("账号：");
        String name = scanner.next();
        System.out.print("密码：");
        String password = scanner.next();
        scanner.nextLine();
        System.out.print("备注：");
        String description = scanner.nextLine();
        account.setId(UUIDUtils.fastUUID());
        account.setSource(source);
        account.setUrl(url);
        account.setName(name);
        account.setPassword(password);
        account.setDescription(description);
        accountManager.saveAccount(account);
    }

    private static void searchAccount() {
        Account account = new Account();
        System.out.print("账号来源：");
        account.setSource(scanner.next());
        List<Account> accounts = accountManager.searchAccount(account);
        accountManager.showAccount(accounts);
    }


}
